package com.example.zxq_server.whbgy.adpterpack;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.example.zxq_server.whbgy.R;
import com.example.zxq_server.whbgy.adpterpack.adptermodel.GroupModel;
import com.example.zxq_server.whbgy.adpterpack.adptermodel.PhoneModel;

import java.util.List;

/**
 * Created by zxq_Server on 2017/10/5.
 */

public class GroupAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    public final static int TITLE_TYPE = 1;
    public final static int CONTENT_TYPE = 2;


    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public GroupAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TITLE_TYPE, R.layout.item_file);
        addItemType(CONTENT_TYPE, R.layout.item_phonefile);

    }


    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity item) {
        int type = helper.getItemViewType();
        final Context context = helper.getConvertView().getContext();
        switch (type) {
            case TITLE_TYPE: {
                final GroupModel groupModel = (GroupModel) item;
                helper.setText(R.id.file_name, groupModel.getGroupName());
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int pos = helper.getAdapterPosition();
                        if (groupModel.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });


            }
            break;
            case CONTENT_TYPE: {
                final PhoneModel phoneModel = (PhoneModel) item;
                helper.setText(R.id.phone_name, phoneModel.getPhoneName());
                helper.setText(R.id.phone_number, phoneModel.getPhoneNumber());
                helper.getView(R.id.phone_call).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneModel.getPhoneNumber()));
                        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            Toast.makeText(context,"未获取电话拨打权限!",Toast.LENGTH_LONG).show();
                            return;
                        }
                        context.startActivity(intent);
                     }
                 });

             }
                 break;

         }


    }

    @Override
    protected int getDefItemViewType(int position) {

        return super.getDefItemViewType(position);
    }


    @Override
    public int getItemViewType(int position) {

        return super.getItemViewType(position);
    }
}
